﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using NutriLeaf.Web.Core.Height;
using NutriLeaf.Web.Core.Weight;

namespace NutriLeaf.Web.Core.BMI
{
    public class BMI
    {
        public const int NormalBMI = 23;

        public static decimal CalculateBMI(decimal weight, decimal height, Height.HeightUnits heightUnits, Weight.WeightUnits weightUnits)
        {
            //convert to english system
            if (heightUnits == Height.HeightUnits.Centimeters)
            {
                height = Height.Converter.Convert(height, Height.HeightUnits.Centimeters, Height.HeightUnits.Inches);
            }


            //convert to english system
            if (weightUnits == Weight.WeightUnits.Kilograms)
            {
                weight = Weight.Converter.Convert(weight, Weight.WeightUnits.Kilograms, Weight.WeightUnits.Pounds);
            }

            //calculate
            //bmi formula

            decimal bmi = (weight / (height * height)) * 703;

            //return
            return bmi;
        }

        public static WeightCategory CalculateWeightCategory(decimal weight, decimal height, Height.HeightUnits heightUnits, Weight.WeightUnits weightUnits)
        {
            return CalculateWeightCategory(CalculateBMI(weight, height, heightUnits, weightUnits));
        }

        public static WeightCategory CalculateWeightCategory(decimal bmi)
        {
            if (bmi <= (decimal)18.5)
            {
                return WeightCategory.Underweight;
            }
            else if (bmi > (decimal)18.5 && bmi <= (decimal)24.99)
            {
                return WeightCategory.Normal;
            }
            else if (bmi >= 25 && bmi <= (decimal)29.99)
            {
                return WeightCategory.Overweight;
            }
            else if (bmi >= 30 && bmi <= (decimal)34.99)
            {
                return WeightCategory.Obesity1;
            }
            else if (bmi >= 35 && bmi <= (decimal)39.99)
            {
                return WeightCategory.Obesity2;
            }
            else
            {
                return WeightCategory.MorbidObesity;
            }

        }

        public static decimal CalculateRecommendedWeight(decimal height, HeightUnits heightUnits)
        {
            if (heightUnits == HeightUnits.Centimeters)
            {
                height = Height.Converter.Convert(height, HeightUnits.Centimeters, HeightUnits.Inches);
            }
            return ((NormalBMI * height * height) / 703);
        }


    }
}